#include "lastvideo.h"


//视频切割，指定帧到指定帧进行切割
lastVideo::lastVideo(QObject *parent)
    :QObject(parent)
{

    m_timer = new QTimer(this);
}

lastVideo::~lastVideo()
{
    exit();
}

bool lastVideo::cutVideo(const QString &inputVideoPath, const QString &outputVideoPath, int a, int b)
{
    cv::VideoCapture cap(inputVideoPath.toStdString());
    if (!cap.isOpened()) {
        qDebug() << "无法打开输入视频文件: " << inputVideoPath;
        return false;
    }

    double fps = cap.get(cv::CAP_PROP_FPS);
    int frameWidth = static_cast<int>(cap.get(cv::CAP_PROP_FRAME_WIDTH));
    int frameHeight = static_cast<int>(cap.get(cv::CAP_PROP_FRAME_HEIGHT));

    // 使用 X264 编码
    cv::VideoWriter writer(outputVideoPath.toStdString(), cv::VideoWriter::fourcc('X', '2', '6', '4'), fps, cv::Size(frameWidth, frameHeight));
    if (!writer.isOpened()) {
        qDebug() << "无法创建输出视频文件: " << outputVideoPath;
        cap.release();
        return false;
    }
    cap.set(cv::CAP_PROP_POS_FRAMES, a);
    int currentFrame = a;
    cv::Mat frame;
    while (cap.read(frame) && currentFrame <= b) {
        writer.write(frame);
        currentFrame++;
    }
    cap.release();
    writer.release();
    return true;
}

void lastVideo::setfile(QPair<QPair<QString, QString>, QPair<int, int> > file)
{
    listfileMutex.lock();
    listfile.append(file);
    listfileMutex.unlock();
}

double lastVideo::getVideoFPS(const std::string &videoPath)
{
    cv::VideoCapture cap(videoPath);
    if (!cap.isOpened()) {
        qDebug() << "无法打开视频文件: " << QString::fromStdString(videoPath);
        return -1;
    }
    double fps = cap.get(cv::CAP_PROP_FPS);
    cap.release();
    return fps;
}

int lastVideo::getTotalFrames(const std::string &videoPath)
{
    cv::VideoCapture cap(videoPath);
    if (!cap.isOpened()) {
        qDebug() << "无法打开视频文件: " << QString::fromStdString(videoPath);
        return -1;
    }
    int totalFrames = static_cast<int>(cap.get(cv::CAP_PROP_FRAME_COUNT));
    cap.release();
    return totalFrames;
}

void lastVideo::startRun()
{

    m_timer->start(1000);
    connect(m_timer,&QTimer::timeout,this,[this](){
        if (exitFlag) {
            m_timer->stop();
            return;
        }
        if(start && !listfile.isEmpty())
        {
            start = false;
            QPair<QPair<QString,QString>,QPair<int,int>> file;
            listfileMutex.lock();
            file = listfile.at(0);
            listfile.removeFirst();
            listfileMutex.unlock();
            QString inputVideoPath = file.first.first;
            QString outputVideoPath = file.first.second+QString::number(file.second.first)+"_"+QString::number((file.second.second-file.second.first)/getVideoFPS(inputVideoPath.toStdString()),'f', 2)+"s.mp4";
            int a = file.second.first;
            int b = file.second.second;
            qDebug()<<"裁减"<<inputVideoPath<<outputVideoPath<<a<<b;

            //多裁减2秒
            if(getTotalFrames(inputVideoPath.toStdString())>b+getVideoFPS(inputVideoPath.toStdString()))
                cutVideo(inputVideoPath, outputVideoPath, a, b+getVideoFPS(inputVideoPath.toStdString()));
            else
                cutVideo(inputVideoPath, outputVideoPath, a, b);
            start = true;
        }
    });
}

void lastVideo::exit()
{
    exitFlag = true;
    // 确保定时器停止
    if (m_timer->isActive()) {
        m_timer->stop();
    }
}





